package org.example;

import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.example.service.UserManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {

	/**
	 * You need hsql, commons-logging, log4j, spring and ibatis 3
	 * @throws Exception
	 * 连接hsqldb  注意是mem模式 保存在内存中，当Releasing JDBC Connection 会释放
	 */
	public static void initializeDatabase() throws Exception {
		Class.forName("org.hsqldb.jdbcDriver").newInstance();
		Connection conn = DriverManager.getConnection("jdbc:hsqldb:mem:test", "sa", "");
		try {
			ScriptRunner runner = new ScriptRunner(conn);
			runner.setErrorLogWriter(null);
			runner.setLogWriter(null);
			Reader reader = Resources
					.getResourceAsReader("org/example/db.sql");
			runner.runScript(reader);
		} finally {
			if (conn != null)
				conn.close();
		}
	}

	public static void main(String[] _args) throws Exception {

		initializeDatabase();
		String[] applicationContexts = {"org/example/applicationContext-reousce.xml","org/example/applicationContext-dao.xml","org/example/applicationContext-service.xml"};
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:org/example/applicationContext*.xml");
		UserManager service = (UserManager) ctx.getBean("userManager");
	
		
		
		service.exec();

		//并发
//		new Thread(new Hilo(service)).start();
//		new Thread(new Hilo(service)).start();
//		new Thread(new Hilo(service)).start();
//		new Thread(new Hilo(service)).start();
//		new Thread(new Hilo(service)).start();
//		new Thread(new Hilo(service)).start();
	}
}